angular.module('Controller')

.controller('QgyContractManageCtrl', ($scope,NgTableParams,menuManage,wfAlert,$localStorage,commonUtil,commonConstant,$rootScope,$uibModal,$http) ->
    initSearchData = ()->
        $scope.search = {
            apartmentId:$localStorage['accountStore'],
            searchStr:undefined
            signTimeStart:undefined
            signTimeEnd:undefined
            status:[]
            more10Day:undefined
            lease:undefined
            useActivity:undefined
        }
    initSearchData()
    $scope.enabledList=commonConstant.enabledList(true)
    $scope.payWayList=commonConstant.payWayList(true)
    $scope.contractStatusList=commonConstant.contractStatusList(true)
    $scope.appointmentStatusList=commonConstant.appointmentStatusList(true)
    $scope.leaseList=commonConstant.leaseList(true)
    loadApartment=()->
        $scope.apartmentList=[{id:undefined,name:'全部'}]
        $http.get('/apartment',params:{page:0,size:999,enabled:true})
        .success (resp)->
            if resp.code==0
                $scope.apartmentList=$scope.apartmentList.concat(resp.message.content)
            else
                console.log(resp.message)
        .error (err)->
            console.log(err)
    loadApartment()
    $scope.tableParams = new NgTableParams(
            {page: 1, count: commonConstant.pageTableCount},
            {
                counts: [5, 10, 20],
                getData: ($defer, params)->
                    $scope.doQuery($defer,params)
                    return ;
            }
    )

    #根据传入的动作和要操作的id更新Array
    $scope.updateSelection=($event, id)->
        checkbox = $event.target;
        if checkbox.checked && $scope.search.status.indexOf(id) == -1
            $scope.search.status.push(id)
        if !checkbox.checked && $scope.search.status.indexOf(id) != -1
            idx = $scope.search.status.indexOf(id)
            $scope.search.status.splice(idx, 1)

    #返回true false
    $scope.isSelected=(id)->
        return $scope.search.status.indexOf(id) >= 0

    $scope.doQuery=($defer, params)->
        status = undefined
        if $scope.search.status.length > 0
            status = $scope.search.status.join(",")
        reqData={
            page: params.page() - 1,
            size: params.count(),
            apartmentId:$scope.search.apartmentId,
            searchStr:$scope.search.searchStr,
            lease:$scope.search.lease,
            status:status
        }
        if $scope.search.more10Day==true
            reqData.more10Day=true
        if $scope.search.useActivity==true
            reqData.useActivity=true
        if $scope.search['signTimeStart']
            reqData['signTimeStart']=moment($scope.search['signTimeStart']).format('YYYY-MM-DD')
        if $scope.search['signTimeEnd']
            reqData['signTimeEnd']=moment($scope.search['signTimeEnd']).format('YYYY-MM-DD')
        $http.get('/contract',params:reqData)
        .success (resp)->
            if resp.code==0
                $scope.tableData=resp.message.content
                params.total(resp.message.totalElements)
                $defer.resolve($scope.tableData)
            else
                console.log(resp.message)
        .error (err)->
            console.log(err)
    _.extend $scope,
        newData: ->
            data={type:1,enabled:1}
            showModal('new',data)
        editData:(item)->
            if item.status in [2,4]
                $rootScope.pageType='contract-edit'
                $rootScope.pageParams=angular.copy({contractId:item.id})
                menuManage.goState('contract-edit',true)
        viewData:(item)->
            return
            theData=angular.copy(item)
            showModal('view',theData)
        goDetail:(item)->
            $rootScope.pageType='contract-detail'
            $rootScope.pageParams={contractId:item.id}
            menuManage.goState('contract-detail',true)
        resetSearch: ->
            initSearchData()
        queryData: (keepPage)->
            if(keepPage != true)
                $scope.tableParams.page(1)
            $scope.tableParams.reload()
        removeData:(item)->
            $http.delete('/contract/'+item.id)
            .success((resp)->
                if resp.code==0
                    wfAlert.toastInfo('合同信息删除成功');
                    $scope.queryData();
                else
                    wfAlert.toastError(resp.message)
            )

    showModal=(type,data)->
        $scope.modal = $uibModal.open({
            animation: true,
            appendTo: $("#modalContent:eq(0)"),
            controller: 'QgyAppointManageEditCtrl',
            templateUrl: 'templates/business-manage/appointment-manage/appointment-manage-modal.html',
            resolve: {
                items:  ()->{type: type, data: data}
            }
        })
        $scope.modal.result.then((modalData)->
            $scope.queryData()
        )
)